package cn.snoworld.service.impl;

import cn.snoworld.entity.Comment;
import cn.snoworld.entity.relation.ArticleComment;
import cn.snoworld.mapper.CommentMapper;
import cn.snoworld.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    @Autowired
    private CommentMapper commentMapper;

    @Transactional
    public boolean save(Integer articleId, Comment entity) {
        // 新增数据
        baseMapper.insert(entity);

        // 关联数据
        ArticleComment articleComment = new ArticleComment(articleId, entity.getId());
        baseMapper.insertRelationForArticle(articleComment);

        return true;
    }

    @Override
    public List<Comment> getCommentListByArticleId(Integer articleId) {
        return commentMapper.selectCommentListByArticleId(articleId);
    }
}
